﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using RollBackLib.src.core;
using log4net;
using RollBack.src.ui;
using System.Threading;

namespace RollBackUI.src.ui.Dialogs.Preferences
{
    public partial class StandardEmailDetail : UserControl
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(StandardEmailDetail));
        private EmailAndSmtpTable m_emailAndSmtpDetails;

        public StandardEmailDetail()
        {
            InitializeComponent();
        }

        internal void populateEmailDetails(RollBackLib.src.core.EmailAndSmtpTable emailAndSmtp)
        {
            m_emailAndSmtpDetails = emailAndSmtp;
            EmailEntry gmail = null;
            EmailEntry yahoo = null;
            EmailEntry hotmail = null;

            if (emailAndSmtp != null && emailAndSmtp.m_emailTable != null)
            {
                foreach (EmailEntry e in emailAndSmtp.m_emailTable.m_EmailList)
                {
                    String lower = e.m_EmailId.ToLowerInvariant();
                    if (lower.Contains("@gmail.") && gmail == null)
                    {
                        gmail = e;
                        m_gmailIdTextBox.Text = e.m_EmailId;
                        m_gmailPassword.Text = e.m_Password;
                    }

                    if (lower.Contains("@yahoo.") && yahoo== null)
                    {
                        yahoo = e;
                        m_yahooIdTextBox.Text = e.m_EmailId;
                        m_yahooPassword.Text = e.m_Password;
                    }

                    if (lower.Contains("@hotmail.") && hotmail == null)
                    {
                        hotmail = e;
                        m_hotmailIdTextBox.Text = e.m_EmailId;
                        m_hotmailPassword.Text = e.m_Password;
                    }

                }
            }
            ChangeCheckboxStyle(checkBox1);
            ChangeCheckboxStyle(checkBox2);
            ChangeCheckboxStyle(checkBox3);
        }

        private static void ChangeCheckboxStyle(CheckBox cbox)
        {
            cbox.Text = "Verified";
            //cbox.ForeColor = Color.Red;
            cbox.CheckState = CheckState.Indeterminate;
        }

        private void m_verifyEmailButton_Click(object sender, EventArgs e)
        {
            Label l = m_statusLabel;
            verifyEmailId(m_gmailIdTextBox.Text,m_gmailPassword.Text, checkBox1, l);
            verifyEmailId(m_yahooIdTextBox.Text, m_yahooPassword.Text, checkBox2, l);
            verifyEmailId(m_hotmailIdTextBox.Text, m_hotmailPassword.Text, checkBox3, l);
        }

        /// <summary>
        /// Verify the email id by sending a test email and updating the labels and checkboxes.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cbox"></param>
        /// <param name="l"></param>
        private void verifyEmailId(String id, String password,CheckBox cbox, Label l)
        {
            if (id.Length > 0 && password.Length > 0)
            {
                l.Text = "Verifying " + id + "   ....";
                ChangeCheckboxStyle(cbox);
                Application.DoEvents();
                using (BusyCursor bc = new BusyCursor(this))
                {
                    try
                    {
                        EmailAndSmtpTable.SendEmail("Email Verification", "Verification email from Rollback", "Verifying EmailId", id, password);
                        cbox.CheckState = CheckState.Checked;
                        l.Text = "Verified " + id;
                        cbox.Enabled = true;
                        Application.DoEvents();
                    }
                    catch (Exception ex)
                    {
                        String msg = "Error verifying EmailId <" + id + ">";
                        log.Warn(msg, ex);
                        l.Text = msg; ;
                        AppUI.ShowException(msg, ex);
                    }
                }
            }
        }

        internal EmailAndSmtpTable getEmailAndSmtpData()
        {
			 EmailAndSmtpTable table = new EmailAndSmtpTable();
            if (m_emailAndSmtpDetails != null)
            {
               
                EmailEntry e = new EmailEntry(m_gmailIdTextBox.Text, m_gmailPassword.Text, EmailAndSmtpTable.gmailSmtpKey);
                table.addEmailEntry(e);

                e = new EmailEntry(m_yahooIdTextBox.Text, m_yahooPassword.Text, EmailAndSmtpTable.yahooSmtpKey);
                table.addEmailEntry(e);

                e = new EmailEntry(m_hotmailIdTextBox.Text, m_hotmailPassword.Text, EmailAndSmtpTable.hotmailSmtpKey);
                table.addEmailEntry(e);
            }
            return table;
        }
    }
}
